package dnsmadejava;

import java.net.*;
import java.io.*;

/**
 *
 * @author user@binaries
 * Copyright 2010 Binaries Group Pte Ltd

Licensed under the Apache License, Version 2.0 (the "License"); you
may not use this file except in compliance with the License. You may
obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
 */
public class Main {

    public static void main(String[] args) throws Exception {
        String username = args[0];
        String password = args[1];
        String id = args[2];
        String inputLine = "";

        try {
            URL dyndns = new URL("http://checkip.dyndns.com/");
            URLConnection ddns = dyndns.openConnection();
            BufferedReader in = new BufferedReader(
                    new InputStreamReader(
                    ddns.getInputStream()));
            inputLine = in.readLine();

            inputLine = inputLine.substring(76, 90); // obtain the ipaddress
            System.out.println(inputLine);
            in.close();

        } catch (Exception e) {
            System.out.println("Fail to connect to http://checkip.dyndns.com/");
            System.out.println("Unable to get local ip address");
        }

        try {
            String newURL = "http://www.dnsmadeeasy.com/servlet/updateip?username="
                    + username + "&password=" + password + "&id=" + id + "&ip=" + inputLine;

            URL mydns = new URL(newURL);
            BufferedReader reply = new BufferedReader(
                    new InputStreamReader(
                    mydns.openConnection().getInputStream()));

            System.out.println(reply.readLine());
            reply.close();
        } catch (Exception e) {
            System.out.println("Unable to update new ip address");
        }
    }
}
